﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class FruitList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _s_title;
        public string _s_ddltype;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化全局变量值
            _s_title = Request["s_title"] == null ? "" : Request["s_title"];

            tb_search_title.Text = _s_title;

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxGamesConfig>();
                SetupCondition(ref criteria);
                criteria.AddOrder(Order.Desc("Id")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount));
            criteria.Add(Expression.Eq("GameType", "fruit"));

            if (!string.IsNullOrEmpty(_s_title))
            {
                criteria.Add(Expression.Like("Title", "%" + _s_title + "%"));//
            }
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxGamesConfig>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }

        protected string ShowLink(string uid, string status)
        {
            string retString = null;

            if (status == "SETTING")
            {
                retString = "<a href='/SystemManage/Weixin/Interaction/OptFruit.aspx?uid=" + uid + "&opt=open' target='ajaxTodo' title='活动开启后，奖品活动内容不得更改。请确定要启动'>启动</a>&nbsp;&nbsp;";
            }
            else if (status == "RUNNING")
            {
                retString = "<a href='/SystemManage/Weixin/Interaction/OptFruit.aspx?uid=" + uid + "&opt=pause' target='ajaxTodo' title='请确定要暂停'>暂停</a>&nbsp;&nbsp;" +
                    "<a href='/SystemManage/Weixin/Interaction/OptFruit.aspx?uid=" + uid + "&opt=close' target='ajaxTodo' title='活动结束后，活动不能再启动。请确定要结束'>结束</a>&nbsp;&nbsp;";
            }
            if (status == "PAUSE")
            {
                retString = "<a href='/SystemManage/Weixin/Interaction/OptFruit.aspx?uid=" + uid + "&opt=open' target='ajaxTodo' title='请确定要启动'>启动</a>&nbsp;&nbsp;" +
                    "<a href='/SystemManage/Weixin/Interaction/OptFruit.aspx?uid=" + uid + "&opt=close' target='ajaxTodo' title='活动结束后，活动不能再启动。请确定要结束'>结束</a>&nbsp;&nbsp;";
            }

            retString += "<a href='/SystemManage/Weixin/Interaction/DeleteFruit.aspx?ids=" + uid + "' target='ajaxTodo' title='请确定要删除'>删除</a>&nbsp;&nbsp";

            if (status != "SETTING")
            {
                retString += "<a href='/SystemManage/Weixin/Interaction/FruitStatList.aspx?uid=" + uid + "' target='navTab' title='统计水果达人' rel='statfruit'>中奖统计</a>&nbsp;&nbsp";
            }

            return retString;
        }
    }
}
